package com.lantosec.hmrp.modules.notify.service.impl;

import com.lantosec.hmrp.modules.notify.entity.NotifyType;
import com.lantosec.hmrp.modules.notify.mapper.NotifyTypeMapper;
import com.lantosec.hmrp.modules.notify.service.INotifyTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.notify.vo.NotifyTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

/**
 * <p>
 * 通知分类 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-05-22
 */
@Service
@Primary
@Log4j2
public class NotifyTypeServiceImpl extends ServiceImpl<NotifyTypeMapper, NotifyType> implements INotifyTypeService {

    @Override
    public void saveAll(NotifyTypeVO vo) {
        NotifyType entity = vo.convertToEntity();
        String id = this.queryMaxId(entity.getParentId());
        entity.setNotifyTypeId(id);
        this.save(entity);
    }

    public String queryMaxId(String parentId) {
        String notifyTypeId = null;
        if(StringUtils.isNotEmpty(parentId)){
            notifyTypeId = baseMapper.queryMaxIdByParentId(parentId);
            return StringUtils.isEmpty(notifyTypeId) ? parentId + "001" : notifyTypeId;
        }else{
            notifyTypeId = baseMapper.queryMaxId();
            return StringUtils.isEmpty(notifyTypeId)? "001" : notifyTypeId;
        }
    }
}
